<?php
	include("library/vars.php");
	
	$uploadDir = 'artwork/';
	
	if(isset($_POST['submit']) && $_FILES['picture']['size'] > 0) {
		// If the form is submited and the artwork is given...
	
		$show_name = addslashes($_POST['show_name']);
		$show_nicename = str_replace('&', 'and', $show_name);
		$show_nicename = strtolower(preg_replace('/\W/','',$show_nicename));
		$season_name = $_POST['season_name'];
	
		$fileName = $_FILES['picture']['name'];
		$tmpName = $_FILES['picture']['tmp_name'];
		$fileSize = $_FILES['picture']['size'];
		$fileType = $_FILES['picture']['type'];

		// get the file extension first
		$ext      = substr(strrchr($fileName, "."), 1); 
		
		if (!($ext == "png" OR $ext == "jpg" OR $ext == "gif")) {
			header( 'Location: add_show.php?s=' .$show_nicename. '&m=only_images&n=' . $show_name .'' );
		}
		else {

		// generate the random file name
		$randName = md5(rand() * time());

		// and now we have the unique file name for the upload file
	    $filePath = $uploadDir . $randName . '.' . $ext;

	    // move the files to the specified directory
		// if the upload directory is not writable or
		// something else went wrong $result will be false
	    $result    = move_uploaded_file($tmpName, $filePath);
		if (!$result) {
			echo "Error uploading file";
			exit;
		}
	
		include("library/config.php");
		include("library/opendb.php");
		
		// Check to see if there's already a show with that name
		$query="SELECT * FROM shows WHERE show_name='$show_name'";
		$result = mysql_query($query);
		$num=mysql_numrows($result);
		$getshow = $show_nicename;
		include("library/parse_fix.php");

		if ($num==0) { // If not, let's add the show...
			$query = "INSERT INTO shows (show_name, show_nicename, cast, path, name, size, type) VALUES ('$show_name', '$show_nicename', '$cast', '$filePath', '$fileName', '$fileSize', '$fileType')";
			mysql_query($query) or die('Database Error');
			
			if ($season_name != 0) {
				$query2 = "INSERT INTO seasons (show_ID, season) VALUES ('$show_nicename', '$season_name')";
				mysql_query($query2) or die('Database Error');
				
				if (isset($_POST['import'])){ 
					// import episodes

					include("library/getting_episodes.php");

						if ($number > 0) {

							foreach($arrayShows as $value) {

					    		$episodenumber = trim(substr($value['episode'],-2, 2), " ");
					   			$value['title'] = str_replace(" &amp", "&", $value['title']);
								$value['title'] = str_replace("</a>", "", $value['title']);
								$title = addslashes($value['title']);
								$value['url'] = str_replace(" &amp", "&", $value['url']);
								$value['url'] = str_replace('<a target="_blank" href="', '', $value['url']);
								$value['url'] = str_replace('<a target="visit" href="', '', $value['url']);
								$value['url'] = str_replace('">', '', $value['url']);
								$url = $value['url'];
								
								include("library/get_summary.php");
								$summary = addslashes($summary);

					    		$query3 = "INSERT INTO episodes (show_ID, season_ID, episode, title, description) VALUES ('$show_nicename', '$season_name', '$episodenumber', '$title', '$summary')";
								mysql_query($query3) or die('Database Error');

							}

						}
						else {

						};

				}
			
				include 'library/closedb.php';
				header( 'Location: index.php?s=' .$show_nicename. '&t=' .$season_name. '&m=show_added2' );
			
			}
			else {
				include 'library/closedb.php';
				header( 'Location: index.php?s=' .$show_nicename. '&m=show_added' );
			}
			
		} else {
			include 'library/closedb.php';
			header( 'Location: add_show.php?s=' . $show_nicename . '&m=show_already_exists&n=' . $show_name .'' );
		}
		}
	}

	elseif(isset($_POST['submit'])) { // If the form is submited without artwork
	
		$show_name = addslashes($_POST['show_name']);
		$show_nicename = str_replace('&', 'and', $show_name);
		$show_nicename = strtolower(preg_replace('/\W/','',$show_nicename));
		$season_name = $_POST['season_name'];
	
		include("library/config.php");
		include("library/opendb.php");
		
		// Check to see if there's already a show with that name
		$query="SELECT * FROM shows WHERE show_name='$show_name'";
		$result = mysql_query($query);
		$num=mysql_numrows($result);
		$getshow = $show_nicename;
		
		if ($num==0) { // If not, let's add the show
			include("library/parse_fix.php");
			include("library/get_show_cast.php");
			
			$query = "INSERT INTO shows (show_name, show_nicename, cast) VALUES ('$show_name', '$show_nicename', '$cast')";
			mysql_query($query) or die('Database Error');
			
			if ($season_name != 0) {
				
				
				
				$query2 = "INSERT INTO seasons (show_ID, season) VALUES ('$show_nicename', '$season_name')";
				mysql_query($query2) or die('Database Error');
				
				if (isset($_POST['import'])){ 
					// import episodes

					include("library/getting_episodes.php");
					
						if ($number > 0) {

							foreach($arrayShows as $value) {

					    		$episodenumber = trim(substr($value['episode'],-2, 2), " ");
					   			$value['title'] = str_replace(" &amp", "&", $value['title']);
								$value['title'] = str_replace("</a>", "", $value['title']);
								$title = addslashes($value['title']);
								$value['url'] = str_replace(" &amp", "&", $value['url']);
								$value['url'] = str_replace('<a target="_blank" href="', '', $value['url']);
								$value['url'] = str_replace('<a target="visit" href="', '', $value['url']);
								$value['url'] = str_replace('">', '', $value['url']);
								$url = $value['url'];
								
								include("library/get_summary.php");
								$summary = addslashes($summary);

					    		$query3 = "INSERT INTO episodes (show_ID, season_ID, episode, title, description) VALUES ('$show_nicename', '$season_name', '$episodenumber', '$title', '$summary')";
								mysql_query($query3) or die('Database Error');

							}

						}
						else {

						}

				}
			
				include 'library/closedb.php';
				header( 'Location: index.php?s=' .$show_nicename. '&t=' .$season_name. '&m=show_added2' );
			
			}
			
			else {			

				include 'library/closedb.php';
				header( 'Location: index.php?s=' .$show_nicename. '&m=show_added2' );
			}
			
		} else {
			include 'library/closedb.php';
			header( 'Location: add_show.php?s=' . $show_nicename . '&m=show_already_exists&n=' . $show_name .'' );
		}
	}
	
	else {
		include("library/header.php");
?>
	
<div id="main">
	<div id="seasons">
		<h2><?php echo _("Add TV Show"); ?></h2>
	</div>
	
	<div id="messages">
		<?php include ("library/messages.php");	?>
	</div>
	
	<form method="post" autocomplete="off" id="add_show" action="" enctype="multipart/form-data">
	
		<p><label for="show_name"><?php echo _("Show Name"); ?>:</label> <input name="show_name" type="text" id="show_name" class="{required:true}" value="<?=$name?>" tabindex="1" />
		<input type="hidden" name="MAX_FILE_SIZE" value="2000000" /></p>
		<p><label for="picture"><?php echo _("Artwork"); ?>:</label> <input name="picture" type="file" id="picture" /><br /><small class="info"><?php echo _("200 x 110px fits best"); ?></small></p>
		<p class="info"><?php echo _("If you like, you can add a New Season to this Show right now (optional):"); ?></p>
		<p><label for="season_name"><?php echo _("Season Number"); ?>:</label> <input name="season_name" type="text" id="season_name" style="width:25px" class="{digits:true}" value="<?=$season;?>" tabindex="2" /></p>
		<p><input type="checkbox" name="import" id="import" checked="checked" style="width:auto;margin-left:130px" tabindex="3" /><label for="import" style="float:none;"><?php echo _("Automatically import all episodes for this season from"); ?> <img src="library/images/epguides.png" alt="epguides.com" class="img_logo" /> &amp; <img src="library/images/tv.gif" alt="TV.com" class="img_logo" /></label></p>
	
		<p class="submit2"><input type="submit" name="submit" class="submit" id="submit" value="<?php echo _("Add New Show"); ?>" tabindex="4" /> <?php echo _("or"); ?> <a href="index.php"><?php echo _("Cancel"); ?></a></p>
	
	</form>
	
	<div id="import_notice">
		<p><?php echo _("<strong>Importing episodes...</strong><br />This might take a while."); ?></p>
		<p class="nota"><?php echo _("Please don't close this window"); ?>.</p>
	</div>
	
	<div class="push"></div>

</div><!-- /#main -->
<?php include("library/footer.php");?>
</body>
</html>
<?php } ?>